﻿using System;
using System.Collections.Generic;
using System.Text;
using BaseData;
using BaseData.Common;
using TaxEntity.User;
using TaxData.OaSys;

namespace TaxBiz.OaSys
{
    /// <summary>
    /// 集成系统邮件类
    /// </summary>
    public class EmailBiz
    {
        /// <summary>
        /// 发送邮件
        /// </summary>
        public static bool Send(UserEntity model, string subject, string content, int mailto)
        {
            bool isSendSucc = true;
            try
            {
                isSendSucc = EmailData.Add(model, subject, content, mailto);
            }
            catch(Exception e)
            {
                isSendSucc = false;
            }
            return isSendSucc;
        }

        /// <summary>
        /// 发送邮件
        /// </summary>
        public static bool SendBatch(UserEntity model, string subject, string content, string mailto)
        {
            bool isSendSucc = true;
            mailto = mailto.Replace("@lnds.com", "");
            string[] arrMail = mailto.Split(';');
            foreach (string mailName in arrMail)
            {
                int mailID = UserInfoBiz.GetID(mailName);
                if (mailID > 0)
                {
                    isSendSucc = Send(model, subject, content, mailID);
                }
            }
            return isSendSucc;
        }

        /// <summary>
        /// 发送邮件
        /// </summary>
        public static bool SendBatchByInterface(UserEntity model, string subject, string content, string mailto)
        {
            bool isSuccess = false;
            string url = BaseData.UI.PageContext.GetConfig("OaSysUrl");

            subject = System.Web.HttpContext.Current.Server.UrlEncode(subject);
            content = System.Web.HttpContext.Current.Server.UrlEncode(content);
            subject = System.Web.HttpContext.Current.Server.HtmlEncode(subject);
            content = System.Web.HttpContext.Current.Server.HtmlEncode(content);
            string paramas = string.Format("action=sendemail&subject={0}&content={1}&mailto={2}", subject, content, mailto);
            StringBuilder sb = BaseData.UI.SearchInterface.GetSearchData(paramas, url);
            if (sb != null && sb.ToString() == "1")
            {
                isSuccess = true;
            }
            return isSuccess;
        }
    }
}
